package com.jinzhi.jzapi.courses.Controller;

import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.CoursesDO;
import com.jinzhi.jzweb.pojo.request.CouresesRequest;
import com.jinzhi.jzweb.service.CoursesService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;
import java.util.Optional;


@Slf4j
@Controller
@RequestMapping("/courses")
@Api(value = "资源管理",tags = "资源管理")
public class AssetsController extends BaseController {

    private final CoursesService coursesService;

    public AssetsController(CoursesService coursesService) {
        this.coursesService = coursesService;
    }


    @ApiOperation(value = "资源后台管理页面入口", notes = "资源后台管理页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/resource")
    String getList(Model model,Integer type) {
        String roleName = getRoleNames();
        String authority = "";
        if(roleName.contains("教师")){
            authority = "教师";
        }else if(roleName.contains("管理员")){
            authority = "管理员";
        }else{
            authority = "管理员";
        }
        model.addAttribute("authority", authority);
        model.addAttribute("type", type);
        return "jzweb/resource/resource";
    }



    @ApiOperation(value = "资源后台管理修改页面入口", notes = "资源后台管理修改页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/edit")
    String edit(Model model,Integer type) {
        String roleName = getRoleNames();
        String authority = "";
        if(roleName.contains("教师")){
            authority = "教师";
        }else if(roleName.contains("管理员")){
            authority = "管理员";
        }else{
            authority = "管理员";
        }
        model.addAttribute("authority", authority);
        model.addAttribute("type", type);
        return "jzweb/recycleBin/edit";
    }

    @ApiOperation(value = "资源后台管理新增页面入口", notes = "资源后台管理新增页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/add")
    String add(Model model) {
        String roleName = getRoleNames();
        String authority = "";
        if(roleName.contains("教师")){
            authority = "教师";
        }else if(roleName.contains("管理员")){
            authority = "管理员";
        }else{
            authority = "管理员";
        }
        model.addAttribute("authority", authority);
        return "jzweb/resource/add";
    }


    @ApiOperation(value = "资源后台管理修改页面入口", notes = "资源后台管理修改页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/update")
    String update(Model model) {
        String roleName = getRoleNames();
        String authority = "";
        if(roleName.contains("教师")){
            authority = "教师";
        }else if(roleName.contains("管理员")){
            authority = "管理员";
        }else{
            authority = "管理员";
        }
        model.addAttribute("authority", authority);
        return "jzweb/resource/edit";
    }



    @ApiOperation(value = "新增资源", notes = "新增资源")
    @ResponseBody
    @PostMapping("/save")
    public Result<String> save(@RequestBody CoursesDO courses){
        courses.setCreaterId(this.getUserId());
        courses.setUpdaterId(this.getUserId());
        courses.setCreaterName(this.getUsername());
        return  coursesService.insert(courses)  ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "修改资源", notes = "修改资源")
    @ResponseBody
    @PostMapping("/update")
    public Result<String> update(@RequestBody CoursesDO courses){
        courses.setUpdaterId(this.getUserId());
        return  coursesService.updateById(courses)  ? Result.ok() : Result.fail();
    }


    @ApiOperation(value = "效验排序是否重复", notes = "效验排序是否重复")
    @ResponseBody
    @PostMapping("/effect")
    public Result<String> effect(@RequestBody CoursesDO courses){
        CoursesDO coursesDO = coursesService.selectOne(courses);
        if (Objects.isNull(coursesDO)){
            return Result.build(0,"验证成功");
        }else {
            return Result.build(1,"验证失败");
        }
    }


    @ApiOperation(value = "资源多条件查询", notes = "资源多条件查询")
    @ResponseBody
    @PostMapping("/getCourseList")
    public Result getCourseList(@RequestBody CouresesRequest couresesRequest){
        return coursesService.getCourseList(couresesRequest);
    }


    @ApiOperation(value = "根据id查询资源", notes = "根据id查询资源")
    @ResponseBody
    @GetMapping("/getCourseById")
    public Result Course(Long id){
        return Result.ok(coursesService.selectById(id));
    }



    @ApiOperation(value = "压缩包下载", notes = "压缩包下载")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/fileDownload")
    public  Result  fileDownload(@RequestParam Long coursesId,HttpServletResponse response) throws IOException {
        return coursesService.fileDownload(coursesId,response);
    }

    @ApiOperation(value = "压缩包下载验证", notes = "压缩包下载验证")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/resourceEfficiency")
    public  Result  resourceEfficiency(@RequestParam Long coursesId)  {
        return coursesService.resourceEfficiency(coursesId);
    }


    @ApiOperation(value = "物理删除资源信息", notes = "物理删除资源信息")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/deletedById")
    public  Result deletedById(Long id) {
        return coursesService.removeById(id);
    }




    @ApiOperation(value = "回收站列表页面入口", notes = "回收站列表页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/resourcesRecovery")
    String resourcesRecovery(Model model) {
        String roleName = getRoleNames();
        String authority = "";
        if(roleName.contains("教师")){
            authority = "教师";
        }else if(roleName.contains("管理员")){
            authority = "管理员";
        }else{
            authority = "管理员";
        }
        model.addAttribute("authority", authority);
        return "jzweb/resourcesRecovery/resourcesRecovery";
    }



    @ApiOperation(value = "回收站详情页面入口", notes = "回收站详情页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/recoveryEdit")
    String recoveryEdit() {
        return "jzweb/resourcesRecovery/edit";
    }
}
